package cn.edu.seig.springks.controller;

import cn.edu.seig.springks.pojo.cart;
import cn.edu.seig.springks.pojo.order;
import cn.edu.seig.springks.pojo.product;
import cn.edu.seig.springks.service.detailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 详细信息控制器，处理与商品详情、购物车和订单相关的请求
 */
@RestController
@RequestMapping(value = "/api/detail")
public class detailController {
    @Autowired
    detailService detailService;

    /**
     * 根据商品ID获取商品信息
     *
     * @param p_id 商品ID
     * @return 返回商品对象，包含名称、单价、描述和图片
     */
    @GetMapping(value = "/getProduct/{p_id}")
    public ResponseEntity<product> getProduct(@PathVariable int p_id){
        product product = detailService.getProduct(p_id);
        return ResponseEntity.ok(product);
    }

    /**
     * 将商品添加到购物车
     *
     * @param u_id 用户ID
     * @param p_id 商品ID
     * @param quantity 商品数量
     * @return 返回影响的行数，表示添加到购物车的商品数量
     */
    @PostMapping(value = "/insertCart/{u_id}/{p_id}/{quantity}")
    public ResponseEntity<Integer> insertCart(@PathVariable int u_id,@PathVariable int p_id,@PathVariable int quantity){
        cart cart = new cart(u_id,p_id,quantity);
        int result = detailService.insertCart(cart);
        return ResponseEntity.ok(result);
    }

    /**
     * 购买当前商品
     *
     * @param u_id 用户ID
     * @param p_id 商品ID
     * @param quantity 商品数量
     * @param total 总价
     * @param address 用户地址
     * @return 返回影响的行数，表示订单创建的结果
     */
    @PostMapping(value = "/buyProduct/{u_id}/{p_id}/{quantity}/{total}/{address}")
    public ResponseEntity<Integer> buyProduct(@PathVariable int u_id,@PathVariable int p_id,@PathVariable double total,@PathVariable int quantity,@PathVariable String address){
        int result = detailService.buyProduct(new order(u_id,p_id,quantity,total,address));
        return ResponseEntity.ok(result);
    }
}
